package 数据结构.栈.数组栈;

import 数据结构.数组.Array;
import 数据结构.栈.Stack;

/**
 * @author Veng Su 1344114844@qq.com
 * @date 2018/7/16 16:29
 */
public class ArrayStack<E> implements Stack<E> {
    private Array<E> array;

    ArrayStack(int capacity) {
        array = new Array<>(capacity);
    }

    ArrayStack() {
        array = new Array<>();
    }

    @Override
    public int getSize() {
        return array.getSize();
    }

    @Override
    public boolean isEmpty() {
        return array.isEmpty();
    }

    @Override
    public void push(E e) {
        array.addLast(e);
    }

    @Override
    public E pop() {
        return array.removeLast();
    }

    @Override
    public E peek() {
        return array.getLast();
    }

    public int getCapacity() {
        return array.getCapacity();
    }

    @Override
    public String toString() {
        StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append("ArrayStack: [");
        for (int i = 0; i < array.getSize(); i++) {
            stringBuilder.append(array.get(i));
            if (i!=array.getSize()){
                stringBuilder.append(",");
            }
        }
        stringBuilder.append("] top");

        return stringBuilder.toString();
    }
}
